package com.bioroadlab.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.stereotype.Controller;

import com.bioroadlab.pojo.Patient;

@Controller
public class PatientAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private String term;

	@Override
	public String execute() throws Exception {
		return null;
	}

	@Action("getPatient")
	public String getPatientInfo() {
		List<Patient> patientList = getPatientService().findAllByName(term);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=UTF-8");

		try {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append("[");
			for (Patient patient : patientList) {
				// [{label:"aaa",value:"aaa1"},{label:"bbb",value:"bbbb2"}],
				stringBuilder.append("{\"label\":\"");
				stringBuilder.append(patient.getName()).append(",")
						.append(patient.getGender()).append(",")
						.append(patient.getIdNumber());
				stringBuilder.append("\",\"value\":\"");
				stringBuilder.append(patient.getName()).append(",")
						.append(patient.getGender()).append(",")
						.append(patient.getBirthday()).append(",")
						.append(patient.getIdNumber()).append(",")
						.append(patient.getContact()).append(",")
						.append(patient.getTelephone()).append(",")
						.append(patient.getSn());
				stringBuilder.append("\"},");
			}
			if (stringBuilder.length() > 1) {
				stringBuilder.deleteCharAt(stringBuilder.length() - 1);
			}
			stringBuilder.append("]");
			PrintWriter out = response.getWriter();
			out.print(stringBuilder.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

}
